function Ajax($method, $url, $parmas) {
    // 使用Promise
    return new Promise(function (resolve, reject) {
        let xhr = new XMLHttpRequest();
        let str = "?";
        if ($parmas) {
            for (let key in $parmas) {
                str += `${key}=${$parmas[key]}&`;
            }
        };

        if ($method.toUpperCase() === "GET") {
            xhr.open($method, $url + str.slice(0, -1));
            xhr.send();
        } else {
            xhr.open($method, $url);
            xhr.setRequestHeader("content-type", "application/json");
            xhr.send(JSON.stringify($parmas));
        };

        xhr.addEventListener("readystatechange", function () {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    // 返回成功
                    resolve(JSON.parse(xhr.response));
                } else {
                    // 返回错误
                    reject(JSON.parse(xhr.response));
                }
            }
        });
    });
};


let url1 = "http://studentback.dfbuff.top/getMethod";
let parmas = {
    "type": "图片"
};
let ul = document.querySelector('ul')
// console.log(Ajax("GET", url1, parmas));
Ajax("GET", url1, parmas)
    .then(function (res) {
        // console.log("GET的结果", res);
        // 返回一个Promise
        for (let i = 0; i < res.result.length; i++) {
            let newImg = new Image();
            let li = document.createElement('li');
            newImg.src = "";
            newImg.dataset.src = res.result[i].content
            newImg.classList.add("lazy")
            li.appendChild(newImg)
            // console.log(res.result[i].content);
            ul.appendChild(li);
        }

        let imgs = document.querySelectorAll(".lazy");
        let io = new IntersectionObserver(function (entries) {
            entries.forEach(function (item) {
                if (item.isIntersecting) {
                    item.target.src = item.target.dataset.src;
                    io.unobserve(item.target);
                };
            });
        }, {
            root: document.querySelector("ul"),
            rootMargin: "200px 0px"
        });


        imgs.forEach(function (el) {
            io.observe(el);
        });

    })
    .catch(function (err) {
        console.log(err);
    });